US State GDP Map
with leaflet
This document demonstrates how to create a choropleth map of the United States, where each state is colored based on its per capita GDP in 2022. We will use the leaflet
package in R to create the interactive map.
1 Data Loading and Preparation
First, we load the necessary R libraries for data manipulation, visualization, and working with geospatial data.
{ r} # Load the tidyverse package for data manipulation and visualization. library(tidyverse) # Load the rvest package for web scraping. library(rvest) # Load the chromote package for controlling a headless Chrome browser. library(chromote) # Load the janitor package for data cleaning. library(janitor) # Load the tidygeocoder package for geocoding. library(tidygeocoder) # Load the leaflet package for creating interactive maps. library(leaflet) # Load the sf package for working with simple features. library(sf)
1.1 Loading Geospatial and GDP Data
We will load the US states geospatial data from a GeoJSON file and the GDP data from an Excel file.
{ r} # Load the US states map data from the maps package. library(maps) mapStates = map("state", fill = TRUE, plot = FALSE)
US location data: { r} # Read the US states GeoJSON file. json_data=read_sf("us-states.json")
{ r} # Convert the sf object to a data frame. map_df <- as.data.frame(json_data)
{ r} # Get a glimpse of the mapStates data. glimpse(mapStates)
US 2022 GDP data:
```{ r} # Load the openxlsx and readxl packages for reading Excel files. library(openxlsx) library(readxl)
2 Read the US state GDP data from an Excel file and clean up the state names.
per_gdp_usd=read_excel(‘US state gpd 2022.xlsx’) %>% mutate(names=str_replace(names,‘*’,’’) %>% str_trim() )
```{
r}
# Get a glimpse of the GDP data.
glimpse(per_gdp_usd)
2.1 Merging and Preparing the Data
We will merge the geospatial data with the GDP data.
{ r} # Merge the map data with the GDP data by state name. map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "names")
{ r} # Get a glimpse of the merged data. glimpse(map_df002)
{ r} # Convert the merged data frame back to an sf object. map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
3 Creating the Choropleth Map
Now we can create the choropleth map of the US states.
```{ r} # Create a color palette for the map. pal <- colorNumeric( palette = “Blues”, domain = map_df002$per_gdp_usd )
4 Create a color bin palette for the map.
binpal <- colorBin(“Blues”,map_df002$per_gdp_usd, 20, pretty = FALSE)
5 Create the leaflet map.
m <- leaflet(map_sf002) %>% # Add default OpenStreetMap map tiles. addTiles() %>%# Add polygons for the states. addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1, # Set the fill color based on the per capita GDP. color = ~binpal(per_2022_usd), # Create a popup with information about the state. popup = ~ paste0( “地区:”, name, “
”, “
“,”人均gpd:“, per_2022_usd,”(千 美元)“,”
” ), # Create a label with more detailed information. label = lapply(paste0( “地区:”, “”, map_sf002\(name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002\)per_2022_usd/1000), “K
”, “总gpd 美元:”, round(map_sf002$total_2022_usd), “M
” ), htmltools::HTML) ) %>% # Add a legend to the map. addLegend( position = “bottomright”, title = “人均gpd(美元)”, pal = pal, values = ~per_2022_usd, opacity = 1.0 )
6 Display the map.
m
# Resources
Here are some useful resources for learning more about the data and tools used in this document.
https://www.gunviolencearchive.org/reports/mass-shooting?page=8&year=2023
:::{#quarto-navigation-envelope .hidden}
[Ploting]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyLXRpdGxl"}
[R Handbook]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXItdGl0bGU="}
[HK whisk bar]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uZXh0"}
[China accident map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1wcmV2"}
[Plot]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOnF1YXJ0by1zaWRlYmFyLXNlY3Rpb24tMQ=="}
[ggplot2 in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90LzEgZ2dwbG90Mi5odG1sZ2dwbG90Mi1pbi1S"}
[Plotly in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90LzIgcGxvdGx5Lmh0bWxQbG90bHktaW4tUg=="}
[image processing]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90LzMgaW1hZ2UgcHJvY2Vzc2luZy5odG1saW1hZ2UtcHJvY2Vzc2luZw=="}
[Plot Financial data]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90LzQgRmluYW5jaWFsIGRhdGEuaHRtbFBsb3QtRmluYW5jaWFsLWRhdGE="}
[Chart club]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90LzUgY2hhcnQgY2x1Yi5odG1sQ2hhcnQtY2x1Yg=="}
[Map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOnF1YXJ0by1zaWRlYmFyLXNlY3Rpb24tMg=="}
[Interactive Maps with Leaflet in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90L01hcC8xIGxlYWZsZXQuaHRtbEludGVyYWN0aXZlLU1hcHMtd2l0aC1MZWFmbGV0LWluLVI="}
[China accident map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90L01hcC8yIENoaW5hIGFjY2lkZW50IG1hcCB3aXRoIG1hcHZpZXcuaHRtbENoaW5hLWFjY2lkZW50LW1hcA=="}
[US State GDP Map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90L01hcC8zIGxlYWZsZXQgVVMuaHRtbFVTLVN0YXRlLUdEUC1NYXA="}
[HK whisk bar]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90L01hcC80IEhLIHdoaXNreSBiYXIuaHRtbEhLLXdoaXNrLWJhcg=="}
[World map with GPD per capita]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1zaWRlYmFyOi9QbG90L01hcC81IHdvcmxkIG1hcC5odG1sV29ybGQtbWFwLXdpdGgtR1BELXBlci1jYXBpdGE="}
[Home]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6SG9tZQ=="}
[/index.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2luZGV4Lmh0bWw="}
[intro]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6aW50cm8="}
[Intro]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6SW50cm8="}
[Basic R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6QmFzaWMgUg=="}
[/intro/1 basic R.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzEgYmFzaWMgUi5odG1s"}
[Probability]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UHJvYmFiaWxpdHk="}
[/intro/2 probability.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzIgcHJvYmFiaWxpdHkuaHRtbA=="}
[Statistics]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6U3RhdGlzdGljcw=="}
[/intro/3 Statistics.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzMgU3RhdGlzdGljcy5odG1s"}
[R book]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UiBib29r"}
[/intro/5 R boook.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzUgUiBib29vay5odG1s"}
[Data analytic in R book]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YSBhbmFseXRpYyBpbiBSIGJvb2s="}
[/intro/6 data analytic in R book.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzYgZGF0YSBhbmFseXRpYyBpbiBSIGJvb2suaHRtbA=="}
[Error handling]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RXJyb3IgaGFuZGxpbmc="}
[/intro/7 error handling.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2ludHJvLzcgZXJyb3IgaGFuZGxpbmcuaHRtbA=="}
[Data]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YQ=="}
[data manipulation]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6ZGF0YSBtYW5pcHVsYXRpb24="}
[Input & Output in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6SW5wdXQgJiBPdXRwdXQgaW4gUg=="}
[/data manipulation/1 input ouput.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzEgaW5wdXQgb3VwdXQuaHRtbA=="}
[Data Structures in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YSBTdHJ1Y3R1cmVzIGluIFI="}
[/data manipulation/2 data_structure.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzIgZGF0YV9zdHJ1Y3R1cmUuaHRtbA=="}
[Data Manipulation with tidyverse]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YSBNYW5pcHVsYXRpb24gd2l0aCB0aWR5dmVyc2U="}
[/data manipulation/3 data manipulation with tidyverse.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzMgZGF0YSBtYW5pcHVsYXRpb24gd2l0aCB0aWR5dmVyc2UuaHRtbA=="}
[Data Tables in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YSBUYWJsZXMgaW4gUg=="}
[/data manipulation/4 data table.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzQgZGF0YSB0YWJsZS5odG1s"}
[Recipe in tidymodels]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UmVjaXBlIGluIHRpZHltb2RlbHM="}
[/data manipulation/5 recipe.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzUgcmVjaXBlLmh0bWw="}
[resample]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6cmVzYW1wbGU="}
[/data manipulation/6 resample.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzYgcmVzYW1wbGUuaHRtbA=="}
[Working with SQL Databases in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6V29ya2luZyB3aXRoIFNRTCBEYXRhYmFzZXMgaW4gUg=="}
[/data manipulation/7 SQL database.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzcgU1FMIGRhdGFiYXNlLmh0bWw="}
[Data management]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6RGF0YSBtYW5hZ2VtZW50"}
[/data manipulation/8 data management.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L2RhdGEgbWFuaXB1bGF0aW9uLzggZGF0YSBtYW5hZ2VtZW50Lmh0bWw="}
[Ploting]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UGxvdGluZw=="}
[Plot]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UGxvdA=="}
[ggplot2 in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6Z2dwbG90MiBpbiBS"}
[/Plot/1 ggplot2.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvMSBnZ3Bsb3QyLmh0bWw="}
[Plotly in R]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UGxvdGx5IGluIFI="}
[/Plot/2 plotly.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvMiBwbG90bHkuaHRtbA=="}
[image processing]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6aW1hZ2UgcHJvY2Vzc2luZw=="}
[/Plot/3 image processing.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvMyBpbWFnZSBwcm9jZXNzaW5nLmh0bWw="}
[Plot Financial data]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UGxvdCBGaW5hbmNpYWwgZGF0YQ=="}
[/Plot/4 Financial data.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvNCBGaW5hbmNpYWwgZGF0YS5odG1s"}
[Chart club]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6Q2hhcnQgY2x1Yg=="}
[/Plot/5 chart club.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvNSBjaGFydCBjbHViLmh0bWw="}
[Map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6TWFw"}
[/Plot/Map/1 leaflet.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1Bsb3QvTWFwLzEgbGVhZmxldC5odG1s"}
[Publishing]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UHVibGlzaGluZw=="}
[Publish]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6UHVibGlzaA=="}
[1 Shiny]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6MSBTaGlueQ=="}
[/Publish/1 Shiny/1 shiny weight tracking.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1B1Ymxpc2gvMSBTaGlueS8xIHNoaW55IHdlaWdodCB0cmFja2luZy5odG1s"}
[2 quarto]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6MiBxdWFydG8="}
[/Publish/2 quarto/1 quarto intro.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1B1Ymxpc2gvMiBxdWFydG8vMSBxdWFydG8gaW50cm8uaHRtbA=="}
[3 dashboard]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6MyBkYXNoYm9hcmQ="}
[/Publish/3 dashboard/1 dashboard.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1B1Ymxpc2gvMyBkYXNoYm9hcmQvMSBkYXNoYm9hcmQuaHRtbA=="}
[4 email]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6NCBlbWFpbA=="}
[/Publish/4 email/email.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1B1Ymxpc2gvNCBlbWFpbC9lbWFpbC5odG1s"}
[5 git]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6NSBnaXQ="}
[/Publish/5 git/git.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L1B1Ymxpc2gvNSBnaXQvZ2l0Lmh0bWw="}
[Other]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6T3RoZXI="}
[1 google analytics]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6MSBnb29nbGUgYW5hbHl0aWNz"}
[/other/1 google analytics/1 google analytics.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L290aGVyLzEgZ29vZ2xlIGFuYWx5dGljcy8xIGdvb2dsZSBhbmFseXRpY3MuaHRtbA=="}
[2 web scrap]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6MiB3ZWIgc2NyYXA="}
[/other/2 web scrap/1 web scrap with rvest.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L290aGVyLzIgd2ViIHNjcmFwLzEgd2ViIHNjcmFwIHdpdGggcnZlc3QuaHRtbA=="}
[6 other]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6NiBvdGhlcg=="}
[/other/6 other/1 chromote.html]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWludC1uYXZiYXI6L290aGVyLzYgb3RoZXIvMSBjaHJvbW90ZS5odG1s"}
[Plot]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWJyZWFkY3J1bWJzLVBsb3Q="}
[Map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWJyZWFkY3J1bWJzLU1hcA=="}
[US State GDP Map]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLWJyZWFkY3J1bWJzLVVTLVN0YXRlLUdEUC1NYXA="}
:::{.hidden .quarto-markdown-envelope-contents render-id="Zm9vdGVyLXJpZ2h0"}
This website is built with ❤️ and [Quarto](https://quarto.org/).
:::
:::
:::{#quarto-meta-markdown .hidden}
[US State GDP Map – R Handbook]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLW1ldGF0aXRsZQ=="}
[US State GDP Map – R Handbook]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLXR3aXR0ZXJjYXJkdGl0bGU="}
[US State GDP Map – R Handbook]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLW9nY2FyZHRpdGxl"}
[R Handbook]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLW1ldGFzaXRlbmFtZQ=="}
[with leaflet]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLXR3aXR0ZXJjYXJkZGVzYw=="}
[with leaflet]{.hidden .quarto-markdown-envelope-contents render-id="cXVhcnRvLW9nY2FyZGRkZXNj"}
:::
<!-- -->
::: {.quarto-embedded-source-code}
```````````````````{.markdown shortcodes="false"}
---
title: "US State GDP Map"
subtitle: "with leaflet"
author: "Tony Duan"
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: right
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
code-copy: true
---
This document demonstrates how to create a choropleth map of the United States, where each state is colored based on its per capita GDP in 2022. We will use the `leaflet` package in R to create the interactive map.
# Data Loading and Preparation
First, we load the necessary R libraries for data manipulation, visualization, and working with geospatial data.
```{
r}
# Load the tidyverse package for data manipulation and visualization.
library(tidyverse)
# Load the rvest package for web scraping.
library(rvest)
# Load the chromote package for controlling a headless Chrome browser.
library(chromote)
# Load the janitor package for data cleaning.
library(janitor)
# Load the tidygeocoder package for geocoding.
library(tidygeocoder)
# Load the leaflet package for creating interactive maps.
library(leaflet)
# Load the sf package for working with simple features.
library(sf)
6.1 Loading Geospatial and GDP Data
We will load the US states geospatial data from a GeoJSON file and the GDP data from an Excel file.
{ r} # Load the US states map data from the maps package. library(maps) mapStates = map("state", fill = TRUE, plot = FALSE)
US location data: { r} # Read the US states GeoJSON file. json_data=read_sf("us-states.json")
{ r} # Convert the sf object to a data frame. map_df <- as.data.frame(json_data)
{ r} # Get a glimpse of the mapStates data. glimpse(mapStates)
US 2022 GDP data:
```{ r} # Load the openxlsx and readxl packages for reading Excel files. library(openxlsx) library(readxl)
7 Read the US state GDP data from an Excel file and clean up the state names.
per_gdp_usd=read_excel(‘US state gpd 2022.xlsx’) %>% mutate(names=str_replace(names,‘*’,’’) %>% str_trim() )
```{
r}
# Get a glimpse of the GDP data.
glimpse(per_gdp_usd)
7.1 Merging and Preparing the Data
We will merge the geospatial data with the GDP data.
{ r} # Merge the map data with the GDP data by state name. map_df002 <- merge(map_df, per_gdp_usd, by.x = "name", by.y = "names")
{ r} # Get a glimpse of the merged data. glimpse(map_df002)
{ r} # Convert the merged data frame back to an sf object. map_sf002 <- sf::st_as_sf(map_df002, sf_column_name = "geometry")
8 Creating the Choropleth Map
Now we can create the choropleth map of the US states.
```{ r} # Create a color palette for the map. pal <- colorNumeric( palette = “Blues”, domain = map_df002$per_gdp_usd )
9 Create a color bin palette for the map.
binpal <- colorBin(“Blues”,map_df002$per_gdp_usd, 20, pretty = FALSE)
10 Create the leaflet map.
m <- leaflet(map_sf002) %>% # Add default OpenStreetMap map tiles. addTiles() %>%# Add polygons for the states. addPolygons(smoothFactor = 0.3, fillOpacity = 0.5,weight = 1, # Set the fill color based on the per capita GDP. color = ~binpal(per_2022_usd), # Create a popup with information about the state. popup = ~ paste0( “地区:”, name, “
”, “
“,”人均gpd:“, per_2022_usd,”(千 美元)“,”
” ), # Create a label with more detailed information. label = lapply(paste0( “地区:”, “”, map_sf002\(name, "</b>", "<br/>",
"人均gpd 美元:", round(map_sf002\)per_2022_usd/1000), “K
”, “总gpd 美元:”, round(map_sf002$total_2022_usd), “M
” ), htmltools::HTML) ) %>% # Add a legend to the map. addLegend( position = “bottomright”, title = “人均gpd(美元)”, pal = pal, values = ~per_2022_usd, opacity = 1.0 )
11 Display the map.
m
# Resources
Here are some useful resources for learning more about the data and tools used in this document.
https://www.gunviolencearchive.org/reports/mass-shooting?page=8&year=2023
:::